home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 424_01 / ed_157 / change_case.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-10  |  4.4 KB  |  155 lines

  1. /*
  2.  * Copyright (C) 1992 by Rush Record (rhr@clio.rice.edu)
  3.  * 
  4.  * This file is part of ED.
  5.  * 
  6.  * ED is free software; you can redistribute it and/or modify it under the terms
  7.  * of the GNU General Public License as published by the Free Software Foundation.
  8.  * 
  9.  * ED is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  10.  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  11.  * PARTICULAR PURPOSE.  See the GNU General Public License for more details.
  12.  * 
  13.  * You should have received a copy of the GNU General Public License along with ED
  14.  * (see the file COPYING).  If not, write to the Free Software Foundation, 675
  15.  * Mass Ave, Cambridge, MA 02139, USA.
  16.  */
  17. #include "opsys.h"
  18.  
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21.  
  22. #include "rec.h"
  23. #include "window.h"
  24. #include "ed_dec.h"
  25. #include "handy.h"
  26. #include "buffer.h"
  27.  
  28. /******************************************************************************\
  29. |Routine: change_case
  30. |Callby: edit
  31. |Purpose: Changes the case of all bytes (inclusive) between the current
  32. |         position and another position at a given offset from the current
  33. |         position. Optionally moves to the other end of the region.
  34. |Arguments:
  35. |    offset is the offset from the current position to the other end of the
  36. |            region that is to be affected.
  37. |    otherend is a flag that indicates that the cursor should be left at the
  38. |            other end of the changed region.
  39. |    table is the case-changing table, or NULL if they want capitalization.
  40. \******************************************************************************/
  41. Int change_case(offset,otherend,table)
  42. register Int offset,otherend;
  43. Uchar *table;
  44. {
  45.     register rec_ptr otherrec,rec;
  46.     register Int otherbyt,otherrow,i,nrecs;
  47.  
  48.     if(offset < 0)    /* we are working backwards from current position */
  49.     {
  50.         if(CURREC == BASE->next && !CURBYT)    /* ignore backward at top of file */
  51.             return(0);
  52. /* find the other end of the buffer, backwards */
  53.         otherrec = CURREC;
  54.         otherbyt = CURBYT;
  55.         otherrow = CURROW;
  56.         nrecs = 1;
  57.         while(offset++ < 0)
  58.         {
  59.             if(!otherbyt)
  60.             {
  61.                 if(otherrec->prev == BASE)
  62.                     return(0);
  63.                 otherrec = otherrec->prev;
  64.                 otherbyt = otherrec->length;
  65.                 otherrow--;
  66.                 nrecs++;
  67.             }
  68.             else
  69.                 otherbyt--;
  70.         }
  71. /* alter the record data structures and redisplay */
  72.         if(nrecs == 1)
  73.         {
  74.             rec_chgcas(CURREC,otherbyt,CURBYT,table);    /* was entirely in current record */
  75.             paint(CURROW,CURROW,FIRSTCOL);    /* repaint current record */
  76.         }
  77.         else
  78.         {
  79. /* deal with the database */
  80.             rec = otherrec;
  81.             if(otherbyt != otherrec->length)
  82.                 rec_chgcas(rec,otherbyt,otherrec->length,table);      /* trim the tail off the first record if necessary */
  83.             rec = rec->next;
  84.             for(i = 1;i < nrecs - 1;i++)    /* remove intervening records, if any */
  85.             {
  86.                 rec_chgcas(rec,0,rec->length,table);
  87.                 rec = rec->next;
  88.             }
  89.             if(CURBYT != 0)
  90.                 rec_chgcas(rec,0,CURBYT,table);    /* trim the front off the last record if necessary */
  91. /* fix the display */
  92.             paint(max(TOPROW,otherrow),CURROW,FIRSTCOL);
  93.         }
  94.     }
  95. /* we are deleting forwards from the current position */
  96.     else
  97.     {
  98.         if(CURREC == BASE)    /* ignore forward at end of file */
  99.             return(0);
  100. /* find the other end of the buffer, forwards */
  101.         otherrec = CURREC;
  102.         otherbyt = CURBYT;
  103.         otherrow = CURROW;
  104.         nrecs = 1;
  105.         while(offset-- > 0)
  106.         {
  107.             if(otherrec == BASE)
  108.                 return(0);
  109.             if(otherbyt == otherrec->length)
  110.             {
  111.                 otherrec = otherrec->next;
  112.                 otherbyt = 0;
  113.                 otherrow++;
  114.                 nrecs++;
  115.             }
  116.             else
  117.                 otherbyt++;
  118.         }
  119. /* alter the record data structures and redisplay */
  120.         if(nrecs == 1)
  121.         {
  122.             rec_chgcas(CURREC,CURBYT,otherbyt,table);    /* kill was entirely in current record */
  123.             paint(CURROW,CURROW,FIRSTCOL);    /* repaint current record */
  124.         }
  125.         else
  126.         {
  127. /* deal with the database */
  128.             rec = CURREC;
  129.             if(CURBYT != rec->length)
  130.                 rec_chgcas(CURREC,CURBYT,CURREC->length,table);       /* trim the tail off the first record if necessary */
  131.             rec = rec->next;
  132.             for(i = 1;i < nrecs - 1;i++)    /* remove intervening records, if any */
  133.             {
  134.                 rec_chgcas(rec,0,rec->length,table);
  135.                 rec = rec->next;
  136.             }
  137.             if(otherbyt != 0)
  138.                 rec_chgcas(rec,0,otherbyt,table);    /* do the front on the last record if necessary */
  139. /* fix the display */
  140.             paint(CURROW,min(BOTROW,otherrow),FIRSTCOL);
  141.         }
  142. /* establish new location and column context */
  143.     }
  144.     if(otherend)
  145.     {
  146.         CURROW = otherrow;
  147.         CURREC = otherrec;
  148.         CURBYT = otherbyt;
  149.         fix_scroll();
  150.         WANTCOL = CURCOL;
  151.     }
  152.     return(1);
  153. }
  154.  
  155.